<?php 
namespace app\supermanager\controller;
use think\Request;
use think\Db;
use think\Validate;
/**
 * 营地管理类
 */
class Camp extends \think\Controller
{
	use \app\common\traits\controller\TraitController;
    use \app\common\traits\controller\TraitUpload;
	protected function init(){
		if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
		$this->model = model('Camps');
		$this->list_route = url('/camps');
	}
	/**
	 * 资源数据列表
	 */
	public function index(){
		//分公司
		$filiales = model('filiales')->column('name','id');
		$this->assign('filiales',$filiales);
		//获得营地信息
		$campName = $this->model->getCampName();
		$this->assign('campName',$campName);
		$list = $this->model->getCaravansLists($this->request->get());
	    return view('camp/index',['list'=>$list]);
	}
	/**
	 * 更改资源信息表单
	 */
	public function edit($id){
		//营地类型
		$types = model('CampTypes')->column('name','id');
		$this->assign('types',$types);
		//营地星级
		$grades = model('CampGrades')->column('name','id');
		//营地所在热门城市
		$city = model('Camps')->getCity();
		$this->assign('grades',$grades);
		$list = $this->model->where(['id'=>$id])->find();
	    return view('camp/edit',['list'=>$list,'city'=>$city]);
	}
	/**
	 * 更改资源--->通过审核
	 * @param -99 审核未通过
	 * @param 1 审核通过
	 */
	public function update(Request $request,$id){
		foreach ($info = $request -> post() as &$v) {
			$v = str_replace(' ', '', $v);
		}
		$result = $this->validate($info, 'Camp');
		if(true !== $result){
		 	return $this->error($result);   
		}
		//营地封面
        if ($cover = $request->post('cover')) {
        	$array_info['cover'] = $cover; 
        }
		//营业执照
		if ($license = $request->post('license')) {
			$array_info['license'] = $license;
        }
		$array_info['status'] = 1; 
		$array_info['country'] = $request->post('addressCountry');
		$array_info['province'] = $request->post('addressProvince');
		$array_info['city'] = $request->post('addressCity');
	    if($this->model->updateInfo(array_merge($request->post(['id'=>$id]),$array_info))){
	    	if (!($this->model->where('id',$id)->value('peak_date'))) {
	    		return $this->success('请设置营地的热门季节时间',url('/camps/setcamp/id/{$id}'));
	    	}
			$this->success('更新成功',$this->list_route);
		}
		$this->error('更新失败');
	}
	/**
	 * 未通过审核
	 */
	public function notPass(Request $request,$id){
		foreach ($info = $request -> post() as &$v) {
			$v = str_replace(' ', '', $v);
		}
		$result = $this->validate($info, 'Camp');
		if(true !== $result){
		 	return $this->error($result);   
		}
		//营地封面
        if ($cover = $request->post('cover')) {
        	$array_info['cover'] = $cover; 
        }
		//营业执照
		if ($license = $request->post('license')) {
			$array_info['license'] = $license;
        }
		$array_info['status'] = -99; 
		$array_info['country'] = $request->post('addressCountry');
		$array_info['province'] = $request->post('addressProvince');
		$array_info['city'] = $request->post('addressCity');
	    if($this->model->updateInfo(array_merge($request->post(['id'=>$id]),$array_info))){
			$this->success('更新成功',$this->list_route);
		}
		$this->error('更新失败');
	}
	/**
	 * 增加营地设置
	 */
	public function setCamp($id){
		$campData = $this->model->get(['id'=>$id]);
	    return $this->fetch('camp/set_camp',['campData'=>$campData]);
	} 
	/**
	 * 营地设置信息入库
	 */
	public function postSetCamp(){
		$peak_date = $this -> request -> post('peak_date/a');
		if(false === Validate::is($peak_date['start_date'], 'date') || false === Validate::is($peak_date['end_date'], 'date'))
		{
			return $this -> error('请输入正确的日期格式');
		}
	    $setPeakResult = $this->model->setPeakDate($this->request->post('camp_id/d'),$this->request->post('peak_date/a'));
		if($setPeakResult){ 
			$this->success('设置成功',$this->list_route);
		}
		$this->error('设置失败');
	} 
	 
	//地区插件
    public function getCountry()
    {
        $country = Db::table('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();;
        return json($country);
        exit;
    }
    public function getProvince()
    {
        $addressProvince = $this->request->get('addressCountry');
        $province = Db::table('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = $this->request->get('addressProvince');
        $city = Db::table('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
}


 ?>
 











